# -*- coding: utf-8 -*-

import sys

import ui.overlap_ui as mui # UI界面
from PyQt4 import QtCore, QtGui, Qt
import lxml.etree as etree  # 使用该扩展xml库

from cal_module import core_module

__program__ = u'CRSCD Overlap计算工具'
__version__ = 'v0.0.1'

default_file='config.xml'

def toUniStr(a):
	return QtCore.QString(unicode(a, 'utf-8', 'ignore'))

class main_ui(Qt.QMainWindow, mui.Ui_MainWindow):
    def __init__(self, parent=None):
        super(main_ui, self).__init__(parent)
        self.setupUi(self)
        root = QtGui.QGridLayout()
        root.addWidget(self.groupFile  , 0, 0, 2, 6)
        root.addWidget(self.groupTime  , 2, 0, 2, 6)
        root.addWidget(self.groupParam , 4, 0, 9, 3)
        root.addWidget(self.groupResult, 4, 3, 9, 3)
        self.centralwidget.setLayout(root)

        self.selectBtn.clicked.connect(self.selectParamFile)
        self.loadBtn.clicked.connect(self.loadParamFile)
        self.calculateBtn.clicked.connect(self.calculate)
        self.clearBtn.clicked.connect(self.clearResult)

        self.openFilesPath = ""
        self.core = core_module()

        self.mEdit_file.setText(default_file)
        self.mEdit_safetymargin.setText("15")
        self.mEdit_sep.setText("1")
        self.mEdit_ramp.setText("0")

    def selectParamFile(self):
        files = QtGui.QFileDialog.getOpenFileName(self,
                "QFileDialog.getOpenFileNames()", self.openFilesPath,
                "Xml Files (*.xml);;All Files (*)")
        if files:
            self.mEdit_file.setText(files)

    def updateCtrl(self):
        self.mEdit_t1      .setText(str(self.core.t1       ))
        self.mEdit_t2      .setText(str(self.core.t2       ))
        self.mEdit_t3      .setText(str(self.core.t3       ))
        self.mEdit_realacc .setText(str(self.core.real_acc ))
        self.mEdit_refacc  .setText(str(self.core.ref_acc  ))
        self.mEdit_verr    .setText(str(self.core.verr     ))
        self.mEdit_entryV  .setText(str(self.core.max_speed))
        self.mEdit_ramp    .setText(str(self.core.ramp_a   ))

    def updateValue(self):
        self.core.t1        = self.mEdit_t1      .text().toDouble()[0]
        self.core.t2        = self.mEdit_t2      .text().toDouble()[0]
        self.core.t3        = self.mEdit_t3      .text().toDouble()[0]
        self.core.real_acc  = self.mEdit_realacc .text().toDouble()[0]
        self.core.ref_acc   = self.mEdit_refacc  .text().toDouble()[0]
        self.core.verr      = self.mEdit_verr    .text().toDouble()[0]
        self.core.max_speed = self.mEdit_entryV  .text().toDouble()[0]
        self.core.ramp_a    = self.mEdit_ramp    .text().toDouble()[0]

    def loadParamFile(self):
        self.core.load_xml(unicode(self.mEdit_file.text().toUtf8(), 'utf-8', 'ignore'))
        self.updateCtrl()


    def calculate(self):
        self.updateValue()
        sep     = int(self.core.sep)
        ventry = int(self.core.max_speed)
        safetym = self.mEdit_safetymargin.text().toDouble()[0]
        if (ventry < 80):
            ventry = 80
        if (sep < 1):
            sep = 1
        s1, s2 = self.core.get_overlap(ventry, safetym, self.core.ramp_a, sep)
        self.mEdit_result.setText(s1)
        self.mEdit_process.setText(s2)

    def clearResult(self):
        self.mEdit_result.setText("")
        self.mEdit_process.setText("")

if __name__ == '__main__':
    a = Qt.QApplication(sys.argv)
    curve = main_ui()
    curve.setWindowTitle(__program__+__version__)
    #curve.setWindowIcon(QtGui.QIcon('icon/crsc.JPG'))
    curve.show()
    a.exec_()
